﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using A2DFramework.ContextService;

namespace A2DFramework.ValidationService
{
    public sealed class BrokenRulesContainer
    {
        private CoreContext ctx;
        public BrokenRulesContainer(CoreContext ctx)
        {
            this.ctx = ctx;
        }

        private List<BrokenRule> brokenRules=new List<BrokenRule>();

        public void AddBrokenRule(string key, string msg)
        {
            BrokenRule rule = new BrokenRule();
            rule.Key = key;
            rule.Message = msg;
            this.brokenRules.Add(rule);

            this.ctx.Save();
        }
        public void AddBrokenRule(BrokenRule rule)
        {
            if (rule == null)
                throw new Exception("rule cannot be null");

            this.brokenRules.Add(rule);

            this.ctx.Save();
        }
        public void AddBrokenRules(List<BrokenRule> rules)
        {
            if (rules == null)
                return;

            foreach (var r in rules)
                this.AddBrokenRule(r);
        }
        public List<BrokenRule> BrokenRules
        {
            get
            {
                return brokenRules.AsReadOnly().ToList();
            }
        }
        public override string ToString()
        {
            string msg = "";
            foreach(var r in this.brokenRules)
            {
                msg += string.Format("{0}: {1}\r\n", r.Key, r.Message);
            }
            return msg;
        }
    }
}
